package me.xingyannian.zhaopin.service;

import me.xingyannian.zhaopin.entity.ChangCi;
import me.xingyannian.zhaopin.entity.RegionDegreeJobStatistics;
import me.xingyannian.zhaopin.entity.SubjectStageJobStatistics;
import me.xingyannian.zhaopin.repository.RegionDegreeJobStatisticsRepository;
import me.xingyannian.zhaopin.repository.SubjectStageJobStatisticsRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;

/**
 * JobStatisticsService
 *
 * @author XING&WANG
 * @version 2025/05/03 22:39
 **/
@Service
public class JobStatisticsService {
    private final SubjectStageJobStatisticsRepository subjectStageJobStatisticsRepository;
    private final RegionDegreeJobStatisticsRepository regionDegreeJobStatisticsRepository;



    public JobStatisticsService(SubjectStageJobStatisticsRepository subjectStageJobStatisticsRepository, RegionDegreeJobStatisticsRepository regionDegreeJobStatisticsRepository) {
        this.subjectStageJobStatisticsRepository = subjectStageJobStatisticsRepository;
        this.regionDegreeJobStatisticsRepository = regionDegreeJobStatisticsRepository;
    }
    // 根据场次查询统计数据
    public List<SubjectStageJobStatistics> findSSJSByChangCi(ChangCi changCi) {
        // 先查出结果
        List<SubjectStageJobStatistics> ssjsList = subjectStageJobStatisticsRepository.findAllByChangCi(changCi);
        // 如果不为空对结果重新排序, 按照总数排序降序

        if (ssjsList != null && !ssjsList.isEmpty()) {
            // 排序
            ssjsList.sort(Comparator.comparing(SubjectStageJobStatistics::getTotalJobCount).reversed());
        }
        return ssjsList;
    }
    public List<RegionDegreeJobStatistics> findRDJSByChangCi(ChangCi changCi) {
        // 先查出结果
        List<RegionDegreeJobStatistics> rdjsList = regionDegreeJobStatisticsRepository.findAllByChangCi(changCi);
        // 如果不为空对结果重新排序, 按照总数排序降序

        if (rdjsList != null && !rdjsList.isEmpty()) {
            // 排序
            rdjsList.sort(Comparator.comparing(RegionDegreeJobStatistics::getTotalJobCount).reversed());
        }
        return rdjsList;
    }
    @Transactional
    public void saveSSJSAll(List<SubjectStageJobStatistics> ssjsList) {
        this.subjectStageJobStatisticsRepository.saveAll(ssjsList);
    }

    @Transactional
    public void deleteSSJSByChangCi(ChangCi changCi) {
        this.subjectStageJobStatisticsRepository.deleteAllByChangCi(changCi);
    }
    @Transactional
    public void saveRDJSAll(List<RegionDegreeJobStatistics> rdjsList) {
        this.regionDegreeJobStatisticsRepository.saveAll(rdjsList);
    }

    @Transactional
    public void deleteRDJSByChangCi(ChangCi changCi) {
        this.regionDegreeJobStatisticsRepository.deleteAllByChangCi(changCi);
    }
}
